package com.ruoyi.house.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

/**
 * 房屋Controller
 *
 * @author wang
 * @date 2022-01-11
 */
@RestController
@RequestMapping("/house/house")
public class XlkjHouseController extends BaseController
{
    @Autowired
    private IXlkjHouseService xlkjHouseService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询房屋列表
     */
    @PreAuthorize("@ss.hasPermi('house:house:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjHouse xlkjHouse)
    {
        //筛选已通过的
        xlkjHouse.setStatus("2");
        startPage();
        if(xlkjHouse.getDepartmentId() != null && xlkjHouse.getDepartmentId() != ""){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(xlkjHouse.getDepartmentId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            xlkjHouse.setDepartmentList(str);
        }
        List<XlkjHouse> list = xlkjHouseService.selectXlkjHouseList(xlkjHouse);
        for(XlkjHouse house:list) {
            //图片路径拼接
            if(house.getImg() != null){
                house.setImg(UrlUtils.getUrl() + house.getImg());
            }
            //部门值翻译
            String deptName = sysDeptService.selectDeptById(Long.valueOf(house.getDepartmentId())).getDeptName();
            house.setDepartmentId(deptName);
        }
        return getDataTable(list);
    }

    /**
     * 导出房屋列表
     */
    @PreAuthorize("@ss.hasPermi('house:house:export')")
    @Log(title = "房屋", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjHouse xlkjHouse)
    {
        List<XlkjHouse> list = xlkjHouseService.selectXlkjHouseList(xlkjHouse);
        for(XlkjHouse house:list){
            //图片路径拼接
            house.setImg(UrlUtils.getUrl() + house.getImg());
            //字典类型回显
            house.setUser(DictUtils.getDictLabel(DictStrUtils.USE_TYPE,house.getUser()));
            house.setApartment(DictUtils.getDictLabel(DictStrUtils.APARTMENT_TYPE,house.getApartment()));
            house.setStructure(DictUtils.getDictLabel(DictStrUtils.STRUCTURE_TYPE,house.getStructure()));
            house.setHouseType(DictUtils.getDictLabel(DictStrUtils.HOUSE_TYPE,house.getHouseType()));
        }
        ExcelUtil<XlkjHouse> util = new ExcelUtil<XlkjHouse>(XlkjHouse.class);
        return util.exportExcel(list, "房屋数据");
    }

    /**
     * 房屋导入
     */
    @Log(title = "房屋", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<XlkjHouse> util = new ExcelUtil<XlkjHouse>(XlkjHouse.class);
        List<XlkjHouse> userList = util.importExcel(file.getInputStream());
        for(XlkjHouse house:userList){
            //字典类型翻译
            house.setUser(DictUtils.getDictValue(DictStrUtils.USE_TYPE,house.getUser()));
            house.setApartment(DictUtils.getDictValue(DictStrUtils.APARTMENT_TYPE,house.getApartment()));
            house.setStructure(DictUtils.getDictValue(DictStrUtils.STRUCTURE_TYPE,house.getStructure()));
            house.setHouseType(DictUtils.getDictValue(DictStrUtils.HOUSE_TYPE,house.getHouseType()));
        }
        String message = xlkjHouseService.importDevice(userList);
        return AjaxResult.success(message);
    }

    /**
     * 下载房屋导入模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<XlkjHouse> util = new ExcelUtil<XlkjHouse>(XlkjHouse.class);
        return util.importTemplateExcel("房屋数据");
    }

    /**
     * 获取房屋详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:house:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(xlkjHouseService.selectXlkjHouseById(id));
    }

    /**
     * 新增房屋
     */
    @PreAuthorize("@ss.hasPermi('house:house:add')")
    @Log(title = "房屋", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjHouse xlkjHouse)
    {
        return toAjax(xlkjHouseService.insertXlkjHouse(xlkjHouse));
    }

    /**
     * 修改房屋
     */
    @PreAuthorize("@ss.hasPermi('house:house:edit')")
    @Log(title = "房屋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjHouse xlkjHouse)
    {
        return toAjax(xlkjHouseService.updateXlkjHouse(xlkjHouse));
    }

    /**
     * 删除房屋
     */
    @PreAuthorize("@ss.hasPermi('house:house:remove')")
    @Log(title = "房屋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjHouseService.deleteXlkjHouseByIds(ids));
    }
}
